<?php

namespace App\Admin\Controllers;

use App\Models\Configuration;
use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ConfigurationController extends Controller
{
    //列表
    public function index(Content $content)
    {
        return Admin::content(function (Content $content) {
            $content->header('系统配置');
            $content->body($this->grid());
        });
    }

    //编辑
    public function edit($id, Content $content)
    {
        $type = Configuration::where('id', $id)->value('type');
        return $content->header('系统配置')->description('编辑')->body($this->form($type)->edit($id));
    }

    //编辑保存
    public function update($id)
    {
        $type = Configuration::where('id', $id)->value('type');
        return $this->form($type)->update($id);
    }

    //获取患者数据
    protected function grid()
    {
        return Admin::grid(Configuration::class, function (Grid $grid) {
            $grid->column('id', 'ID')->sortable();
            $grid->column('name', '配置名称');
            $grid->column('en_name', '英文配置名称');
            $grid->column('parameter', '配置参数');
            $grid->column('type', '参数类别')->display(function ($sex) {
                return ($sex == '1') ? '数值' : '图片';
            });
            $grid->column('create_at', '创建时间');

            $grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableView();
            });

            $grid->filter(function($filter){
            });

            $grid->disableCreateButton();

            $grid->tools(function ($tools) {
                // 禁用批量删除按钮
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });
        });
    }

    //修改表单事项
    protected function form($type = '1')
    {
        // 创建一个表单
        return Admin::form(Configuration::class, function (Form $form) use ($type) {
            $form->display('name', '配置名称');
            if($type == '1') {
                $form->text('parameter', '配置参数');
            }else{
                $form->image_url('parameter', '配置参数')->rules('image');
            }

            $form->tools(function (Form\Tools $tools) {
                // 去掉`删除`按钮
                $tools->disableDelete();
            });
        });
    }
}
